package cn.iocoder.yudao.module.call.controller.admin.aiqualitytestinglabel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.call.controller.admin.aiqualitytestinglabel.vo.AiQualityTestingLabelPageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.aiqualitytestinglabel.vo.AiQualityTestingLabelRespVO;
import cn.iocoder.yudao.module.call.controller.admin.aiqualitytestinglabel.vo.AiQualityTestingLabelSaveReqVO;
import cn.iocoder.yudao.module.call.dal.dataobject.aiqualitytestinglabel.AiQualityTestingLabelDO;
import cn.iocoder.yudao.module.call.service.aiqualitytestinglabel.AiQualityTestingLabelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 大模型质检标签")
@RestController
@RequestMapping("/call/ai-quality-testing-label")
@Validated
public class AiQualityTestingLabelController {

    @Resource
    private AiQualityTestingLabelService aiQualityTestingLabelService;

    @PostMapping("/create")
    @Operation(summary = "创建大模型质检标签")
    @PreAuthorize("@ss.hasPermission('call:ai-quality-testing-label:create')")
    public CommonResult<Long> createAiQualityTestingLabel(@Valid @RequestBody AiQualityTestingLabelSaveReqVO createReqVO) {
        return success(aiQualityTestingLabelService.createAiQualityTestingLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新大模型质检标签")
    @PreAuthorize("@ss.hasPermission('call:ai-quality-testing-label:update')")
    public CommonResult<Boolean> updateAiQualityTestingLabel(@Valid @RequestBody AiQualityTestingLabelSaveReqVO updateReqVO) {
        aiQualityTestingLabelService.updateAiQualityTestingLabel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除大模型质检标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ai-quality-testing-label:delete')")
    public CommonResult<Boolean> deleteAiQualityTestingLabel(@RequestParam("id") Long id) {
        aiQualityTestingLabelService.deleteAiQualityTestingLabel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得大模型质检标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ai-quality-testing-label:query')")
    public CommonResult<AiQualityTestingLabelRespVO> getAiQualityTestingLabel(@RequestParam("id") Long id) {
        AiQualityTestingLabelDO aiQualityTestingLabel = aiQualityTestingLabelService.getAiQualityTestingLabel(id);
        return success(BeanUtils.toBean(aiQualityTestingLabel, AiQualityTestingLabelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得大模型质检标签分页")
    @PreAuthorize("@ss.hasPermission('call:ai-quality-testing-label:query')")
    public CommonResult<PageResult<AiQualityTestingLabelRespVO>> getAiQualityTestingLabelPage(@Valid AiQualityTestingLabelPageReqVO pageReqVO) {
        PageResult<AiQualityTestingLabelDO> pageResult = aiQualityTestingLabelService.getAiQualityTestingLabelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiQualityTestingLabelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出大模型质检标签 Excel")
    @PreAuthorize("@ss.hasPermission('call:ai-quality-testing-label:export')")
    @OperateLog(type = EXPORT)
    public void exportAiQualityTestingLabelExcel(@Valid AiQualityTestingLabelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AiQualityTestingLabelDO> list = aiQualityTestingLabelService.getAiQualityTestingLabelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "大模型质检标签.xls", "数据", AiQualityTestingLabelRespVO.class,
                        BeanUtils.toBean(list, AiQualityTestingLabelRespVO.class));
    }

}